#[cfg(test)]
mod fun_test {
    #[test]
    fn same_return() {
        let condition = true;
        let number = if condition {
            5
        } else {
            6
            // "six" // 返回值类型必须相同
        };
        println!("The value of number is: {}", number);
    }

    #[test]
    fn int_return() {
        // let x = y = 6，//这样 x 和 y 的值都是 6；Rust 中不能这样写。
        let x = 5;
        let y = {
            //{}
            let x = 3;
            x + 1
        };

        println!("x {} y {}", x, y)
    }

    #[test]
    fn func_param() {
        let add: fn(i32) -> i32 = |x| x + 1;
        let x = 5;
        let y = operate_handle(add, x);
        println!("x {} y {}", x, y);
    }

    fn operate_handle(p0: fn(i32) -> i32, p1: i32) -> i32 {
        p0(p1)
    }

    fn return_fun() -> fn(i32) -> i32 {
        let add: fn(i32) -> i32 = |x| x + 1;
        return add;
    }
}

//结构体内闭包
#[cfg(test)]
mod structx {
    struct Cacher<T>
    where
        T: Fn(u32) -> u32,
    {
        query: T,
        value: Option<u32>,
    }

    impl<T> Cacher<T>
    where
        T: Fn(u32) -> u32,
    {
        fn new(query: T) -> Cacher<T> {
            Cacher { query, value: None }
        }

        // 先查询缓存值 `self.value`，若不存在，则调用 `query` 加载
        fn value(&mut self, arg: u32) -> u32 {
            match self.value {
                Some(v) => v,
                None => {
                    let v = (self.query)(arg);
                    self.value = Some(v);
                    v
                }
            }
        }
    }

    #[test]
    fn same_return() {}
}
